package org.genntii.smcommon.interceptor;


import org.genntii.smcommon.constant.GatewayConstant;
import org.genntii.smcommon.context.BaseContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class UserInfoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler) throws Exception {

        String userId = request.getHeader(GatewayConstant.USER_ID);

        if(userId != null && !userId.isEmpty()) {
            BaseContext.setCurrentId(Long.valueOf(userId));
        }

        return true;
    }

    @Override
    public void afterCompletion(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, Exception ex) throws Exception {
        BaseContext.removeCurrentId();
    }
}
